;
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");
var request = require("request");

var mysql = new db.mysqlClient("accident");
// 生成产品数据
function genProductInfo() {
    var projects = [];
    plan = {
        "planA": {
            "year": ["0-17"],
            "days": ["1d", "7d", "15d", "30d", "60d", "90d", "365d"],
            "people_count": ["1"],
            "accident_death": ["100000"],
            "accident_medical": ["350000"],
            "exigency_back": ["300000"],
            "death_back": ["300000"],
            "casket_fee": ["8000"],
            "burial_fee": ["16000"],
            "child_return": [""],
            "relate_mourn": [""],
            "care_affairs": [""],
            "nervous_help": ["4000"]

        },
        "planB": {
            "year": ["18-80"],
            "days": ["1d", "7d", "15d", "30d", "60d", "90d", "365d"],
            "people_count": ["1"],
            "accident_death": ["200000"],
            "accident_medical": ["350000"],
            "exigency_back": ["300000"],
            "death_back": ["300000"],
            "casket_fee": ["8000"],
            "burial_fee": ["16000"],
            "child_return": [""],
            "relate_mourn": [""],
            "care_affairs": [""],
            "nervous_help": ["4000"]

        },
        "planC": {
            "year": ["18-80"],
            "days": ["1d", "7d", "15d", "30d", "60d", "90d", "365d"],
            "people_count": ["1"],
            "accident_death": ["600000"],
            "accident_medical": ["350000"],
            "exigency_back": ["300000"],
            "death_back": ["300000"],
            "casket_fee": ["8000"],
            "burial_fee": ["16000"],
            "child_return": [""],
            "relate_mourn": [""],
            "care_affairs": [""],
            "nervous_help": ["4000"]

        },
        "planD": {
            "year": ["18-80"],
            "days": ["1d", "7d", "15d", "30d", "60d", "90d", "365d"],
            "people_count": ["1"],
            "accident_death": ["1000000"],
            "accident_medical": ["500000"],
            "exigency_back": ["500000"],
            "death_back": ["800000"],
            "casket_fee": ["8000"],
            "burial_fee": ["16000"],
            "child_return": ["1200"],
            "relate_mourn": ["1200"],
            "care_affairs": ["1200"],
            "nervous_help": ["4000"]

        }
    }
    for (k in plan) {
        data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["exigency_back"], plan[k]["death_back"], plan[k]["casket_fee"], plan[k]["burial_fee"], plan[k]["child_return"], plan[k]["relate_mourn"], plan[k]["care_affairs"], plan[k]["nervous_help"]])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan[k])
            projects.push(dt);
        }
    }; return projects;

}
function httpRequest(dt) {
    if (dt.year == "0-17") {
        planCd = "nets0224"
    } else {
        if (dt.accident_death == "200000") {
            planCd = "nets0221"
        } else if (dt.accident_death == "600000") {
            planCd = "nets0222"
        } else {
            planCd = "nets0223"
        }
    }
    params = {
        "productNo": "19",
        "insuredSex": "",
        "insuredBirthday": "",
        "insuranceYears": dt.days.replace("d", ""),
        "paymentPeriod": "",
        "guaranteeAmount": "",
        "mult": "0",
        "payIntv": "0",
        "insuyearFlag": "D",
        "payendyearFlag": "",
        "contPlanCode": planCd
    };
    return new Promise(function (resolve, reject) {
        request({
            url: url,
            method: "POST",
            json: false,
            rejectUnauthorized: false,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"

            },
            body: helper.urlencode(params)
        }, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                if (body.indexOf("result") != -1) {
                    price = Number(JSON.parse(body).result).toFixed(2).toString();
                    rs = {
                        "name": "境外申根游保险",
                        "company": "德华安顾人寿",
                        "price": price
                    }
                    resolve(rs);
                }
            }
        })

    })

}
var url = "https://www.ergo-life.cn//Admin/getCALCUData.do";
var main = async function () {
    var data = genProductInfo();
    for (i = 0; i < data.length; i++) {
        dt = data[i]
        var rs = await httpRequest(dt);
        result = Object.assign(dt, rs);
        mysql.insertData(result);
    }
    mysql.close();

}
